<?php
class Privileges_Model_Pagedecision implements Privileges_Data_Mainable{
    
    private $action_handler;
    private $csrf_token;
    
    public function main(array $variables = null) {
        switch($variables['get']['page']){
            case "dodaj":
                return $this->add_privielege($variables);
                break;
            case "wstaw":
                return $this->insert_privilege($variables);
                break;
            case "edytuj":
                return $this->edit_privilege($variables);
                break;
            case "aktualizuj":
                return $this->update_privilege($variables);
                break;
            case "usun":
                return $this->delete_privilege($variables);
                break;
            default :
                unset($variables);
                header("Location: ".SITE_ROOT."przywileje.html");
        }
    }
    
    private function update_privilege(array $variables = null){
        $this->action_handler = new Privileges_Model_Updateprivilege;
        return $this->action_handler->main($variables);
    }
    private function delete_privilege(array $variables = null){
        $this->action_handler = new Privileges_Model_Deleteprivilege;
        return $this->action_handler->main($variables);
    }
    
    private function edit_privilege(array $variables = null){
        if(isset($variables['get']['name'])){
            $this->action_handler = new Privileges_Data_Editprivileges;
            $page['content'] = $this->action_handler->main($variables);
            $this->csrf_token = new Core_Controller_Csrftoken;
            $page['csrf_token'] = $this->csrf_token->main($variables);
            $page['template'] = "./privileges/views/add.php";
            return $page;
        }
        else {
            return $this->delete_privilege($variables);
        }
        
    }
    
    private function insert_privilege(array $variables = null){
        $this->action_handler = new Privileges_Model_Insertprivilegeaction;
        return $this->action_handler->main($variables);
    }
    
    private function add_privielege(array $variables = null){
        $this->action_handler = new Privileges_Data_Addprivilegeform;
        $page['template'] = "./privileges/views/add.php";
        $page['csrf_token'] = $this->action_handler->main($variables);
        return $page;
    }

}
